//
//  YRStatusOriginalView.swift
//  新浪微博
//
//  Created by Viper on 16/6/28.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit
import SnapKit
class YRStatusOriginalView: UIView {
    //声明一个全局的原创微博底部约束的约束 供我们卸载
    var OriginalViewButtonConstraint : Constraint?
    
    //供外界设置数据
    var status : YRStauts? {
        
        didSet{
          //名字
            nameLabel.text = status?.user?.name
            //头像
            headImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            //时间
            timeLable.text = status?.createAtStr
            //来源
            sourceLabel.attributedText = status?.sourceAttr
       
            //微博内容
            contentLable.text = status?.text
            //认证图片
            vipImageView.image = status?.user?.verifiedImage
            
            //等级图片
            expiredImageView.image = status?.user?.mbrankImage
            
            //卸载约束
            OriginalViewButtonConstraint?.uninstall()
            //如果有配图
            if status?.pic_urls?.count > 0 {
                //显示配图 更新约束
               pictureView.hidden = false
                //赋值
                pictureView.pic_urls = status?.pic_urls
                //更新约束
                self.snp_makeConstraints(closure: { (make) in
                    
                    OriginalViewButtonConstraint = make.bottom.equalTo(pictureView).offset(StatusMargin).constraint
                    
                })}else if status?.pic_urls?.count <= 0{
                //隐藏配图
                pictureView.hidden = true
                //更新约束
                self.snp_makeConstraints(closure: { (make) in
                    
                    OriginalViewButtonConstraint = make.bottom.equalTo(contentLable).offset(StatusMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图   
    private func setupUI(){
        
      backgroundColor = UIColor.whiteColor()
        
        //添加控件
       addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(expiredImageView)
        addSubview(timeLable)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contentLable)
        addSubview(pictureView)
        //添加约束
        //头像
        headImageView.snp_makeConstraints { (make) in
            
            make.leading.top.equalTo(self).offset(StatusMargin)
            make.width.height.equalTo(35)
        
        }
        //昵称
        nameLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(headImageView.snp_trailing).offset(StatusMargin)
            make.top.equalTo(headImageView)
       
        }
        //用户等级
        expiredImageView.snp_makeConstraints { (make) in
            
            make.leading.equalTo(nameLabel.snp_trailing).offset(StatusMargin)
            make.centerY.equalTo(nameLabel)
    
        }
        //发布时间
        timeLable.snp_makeConstraints { (make) in
            
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        //来自
        sourceLabel.snp_makeConstraints { (make) in
        
            make.top.equalTo(timeLable)
            make.leading.equalTo(timeLable.snp_trailing).offset(StatusMargin)
            
        }
        //vip认证图片
        vipImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        contentLable.snp_makeConstraints { (make) in
            
            make.leading.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(StatusMargin)

        }
        
        pictureView.snp_makeConstraints { (make) in

//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.leading.equalTo(contentLable)
            make.top.equalTo(contentLable.snp_bottom).offset(StatusMargin)
            
        }
        
        self.snp_makeConstraints { (make) in
            
         OriginalViewButtonConstraint = make.bottom.equalTo(pictureView).offset(StatusMargin).constraint
        }
        
    //临时
        timeLable.text = "时间"
        sourceLabel.text = "来自乐宝"
        
    }
     //MARK: - 懒加载控件
    
    private lazy var headImageView : UIImageView = UIImageView(imageName: "avatar_default_big")
    private lazy var nameLabel : UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
    
    private lazy var expiredImageView : UIImageView = UIImageView(imageName: "common_icon_membership_expired")
    
    private lazy var timeLable : UILabel = UILabel(textColor: ThemeColor, textFont: StatusMargin)
    private lazy var sourceLabel : UILabel = UILabel(textColor: UIColor.grayColor(), textFont: StatusMargin)
    private lazy var vipImageView :UIImageView = UIImageView(imageName: "avatar_vgirl")
    private lazy  var contentLable : UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWidth: SCREENW - 2*StatusMargin)
    
    private lazy var pictureView : YRPictureView = YRPictureView()
    
    
}
